package com.eastfair.builder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ReportSpaceDTO;
import com.eastfair.builder.entity.Exhibitor;
import com.eastfair.builder.entity.ReportSpace;
import com.eastfair.builder.enumeration.ReportSpaceDrawingAuditStatusEnum;
import com.eastfair.builder.enumeration.ReportSpaceReportSpaceStatusEnum;
import com.eastfair.builder.enumeration.ReportSpaceServiceAuditStatusEnum;
import com.eastfair.builder.exceptioncode.BuilderExceptionCode;
import com.eastfair.builder.service.*;
import com.eastfair.builder.vo.ReportSpaceVO;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dto.*;
import com.eastfair.venuebooking.api.ServiceBookingFeign;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dto.ServiceBookingDTO;
import com.eastfair.venueservice.dto.ServiceBookingQuery;
import com.eastfair.venueservice.dto.ServiceBookingUpdateDTO;
import com.eastfair.venueservice.vo.ServiceBookingVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 报馆记录表
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class BuilderServiceBookingServiceImpl implements BuilderServiceBookingService {

    @Resource
    private ServiceBookingFeign serviceBookingFeign;
    @Override
    public Boolean addServiceBooking(List<ServiceBookingDTO> serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.addServiceBooking(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }
    @Override
    public Boolean addServiceBookingAfterReport(List<ServiceBookingDTO> serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.addServiceBookingAfterReport(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

    @Override
    public Boolean saveServiceBooking(List<ServiceBookingDTO> serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.saveServiceBooking(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

    @Override
    public List<ServiceBookingVO> queryServiceBookingList(ServiceBookingQuery serviceBookingQuery) {
        R<List<ServiceBookingVO>> listR = serviceBookingFeign.queryServiceBookingList(serviceBookingQuery);
        if (!listR.getIsSuccess()) {
            throw new BizException(listR.getCode(), listR.getMsg());
        }
        return listR.getData();
    }

    @Override
    public Boolean updateServiceBookingAuditResult(List<ServiceBookingDTO> serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.updateServiceBookingAuditResult(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

    @Override
    public Boolean submitServiceBookingWhenReport(Long reportSpaceId, Long reportSpaceAuditId) {
        R<List<ReportServiceAuditDTO>> listR = serviceBookingFeign.submitServiceBookingWhenReport(reportSpaceId, reportSpaceAuditId);
        if (!listR.getIsSuccess()) {
            throw new BizException(listR.getCode(), listR.getMsg());
        }
        return true;
    }

    @Override
    public Boolean delServiceBooking(ServiceBookingDTO serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.delServiceBooking(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

    @Override
    public Boolean cancelServiceBooking(ServiceBookingUpdateDTO serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.cancelServiceBooking(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

    @Override
    public IPage<ServiceBookingVO> queryServiceBookingManage(PageParams<ServiceBookingQuery> serviceBookingQuery) {
        IPage<ServiceBookingVO> listR = ConvertUtil.convertIPage(serviceBookingFeign.queryServiceBookingManage(serviceBookingQuery).getData(),ServiceBookingVO.class) ;
        return listR;
    }

    @Override
    public Page<ServiceBookingVO> queryServiceBookingOfHome(PageParams<ServiceBookingQuery> serviceBookingQuery) {
        Page<ServiceBookingVO> listR = ConvertUtil.convertPage(serviceBookingFeign.queryServiceBookingOfHome(serviceBookingQuery).getData(),ServiceBookingVO.class) ;
        // todo 处理报馆时间和报馆状态
        List<String> reportVenueSpaceNumberList = listR.getRecords().stream().map(ServiceBookingVO::getReportVenueSpaceNumber).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(reportVenueSpaceNumberList)) {

        }
        return listR;
    }

    @Override
    public Boolean submitServiceBookingToVenue(List<ServiceBookingDTO> serviceBookingDTO) {
        R<Boolean> booleanR = serviceBookingFeign.submitServiceBookingToVenue(serviceBookingDTO);
        if (!booleanR.getIsSuccess()) {
            throw new BizException(booleanR.getCode(), booleanR.getMsg());
        }
        return booleanR.getData();
    }

}
